package com.cmccgz.udacs_landlord_webapp.main.unlock;

import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.bean.ResultBean;
import com.cmccgz.udacs_landlord_webapp.bean.UnlockRecord;

import java.util.List;

import rx.Subscriber;

/**
 * Created by June on 2017-6-19.
 */

public class UnlockRecordPresenter implements UnlockRecordContract.Presenter {

    private final UnlockRecordContract.View mRecordView;

    public UnlockRecordPresenter(UnlockRecordContract.View view) {
        mRecordView = view;
        mRecordView.setPresenter(this);
    }

    @Override
    public void loadRecord(String q_word, String device_id, String start_date, String end_date, final int page) {
        ApiService.getUnlockRecord(q_word, device_id,start_date,end_date, page)
                .subscribe(new Subscriber<ResultBean<List<UnlockRecord>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (page == 1) {
                            mRecordView.refreshFail("数据加载失败");
                        } else {
                            mRecordView.loadmoreFail("数据加载失败");
                        }
                    }

                    @Override
                    public void onNext(ResultBean<List<UnlockRecord>> listResultBean) {
                        if ("0".equals(listResultBean.rsmsg.rscode) && listResultBean.resp != null) {
                            if (page == 1) {
                                mRecordView.refreshSuccess(listResultBean.resp);
                            } else {
                                mRecordView.loadmoreSuccess(listResultBean.resp);
                            }
                        } else {
                            if (page == 1) {
                                mRecordView.refreshFail(listResultBean.rsmsg.rsdesc);
                            } else {
                                mRecordView.loadmoreFail(listResultBean.rsmsg.rsdesc);
                            }
                        }
                    }
                });

    }

    @Override
    public void start() {

    }
}
